<?php
header('Content-type: text/html; charset=utf-8');
date_default_timezone_set('Asia/Chongqing');
define('webroot', str_replace('admin/includes/init.php', '', str_replace('\\', '/', __FILE__)));
define('adminroot', webroot.'admin/');
include '../data/config.php';
if(web_s != 'johnsongod'){
	exit('Hack!');
}
require '../includes/cls_mysql.php';
require '../includes/smarty/Smarty.class.php';
require '../includes/lib_main.php';
require '../lan/common.php';
$db = new cls_mysql(mysql_host, mysql_user, mysql_pass, mysql_db, charset);
$smarty = new Smarty();
$smarty->cache_lifetime = cache_time;
$smarty->template_dir   = '../templates/admin/'.web_template;
$smarty->cache_dir      = '../temp/caches';
$smarty->compile_dir    = '../temp/compiled';
$common['webroot'] = webroot;
$common['web_template'] = web_template;
$common['admin_template'] = admin_template;
$common['charset'] = charset;
$common['pagesize'] = pagesize;
$common['goods_attr'] = get_goods_attr();
$database = array(
	'm_area',
	'm_color',
	'm_condition',
	'm_displacement',
	'm_mileage',
	'm_models',
	'm_price',
	'm_type'
);

//筛选
foreach($database as $v){
	$common[preg_replace('/(m_.*?)/sim', '', $v).'_id'] = isset($_REQUEST[preg_replace('/(m_.*?)/sim', '', $v).'_id']) ? $_REQUEST[preg_replace('/(m_.*?)/sim', '', $v).'_id'] : '';
}

//语言包
$common['lan'] = $lan;

//主导航
$common['navlist'] = $db->getAll("select * from m_nav order by nav_index, nav_id");

//摩托属性
if(!file_exists('../data/attr.php')){
	$str = "<?php\n";
	$str .= getstr($database);
	file_put_contents('../data/attr.php', $str);
}
include '../data/attr.php';

$smarty->assign('common', $common);

function getstr($database){
	global $db;
	$str = '';
	foreach($database as $d){
		$r = $db->getAll("select * from {$d}");
		$str .= "\$common['".preg_replace('/(m_.*?)/sim', '', $d)."list'] = array(\n";
		foreach($r as $k => $v){
			$str .= "	'{$v[preg_replace('/(m_.*?)/sim', '', $d).'_id']}' => array(\n";
			$i = 0;
			foreach($v as $k1 => $v1){
				$str .= "		'{$k1}' => '{$v1}'".($i >= (count($v) - 1) ? '' : ',')."\n";
				$i ++;
			}
			$str .= "	)".($k >= (count($r) - 1) ? '' : ',')."\n";
		}
		$str .= ");\n";
	}
	return $str;
}
//权限检查
function checkpermissions($permissions){
	if($_SESSION['admin_permissions'] != 'all'){
		if(!is_array($_SESSION['admin_permissions'])){
			exit('权限错误');
		}elseif(!in_array($permissions, $_SESSION['admin_permissions'])){
			exit('权限错误');
		}
	}
}
